// Author: Staffan Persson

// JSLint
/*jslint devel: true, browser: true, vars: true */
// Assume ASOBAUDIO and jQuery
/*global ASOBAUDIO: false, $: false, localStorage: false, NativeInterface: false */

ASOBAUDIO.playlist = ( function() {
	var current = '';

	// Load last saved playlist
	function load() {

	}

	// Save current playlist and the current state of the playlist
	function save() {

	}

	// Change current playlist
	function set(newPlaylist) {
		this.save();
		current = newPlaylist;
	}

	return {
		load : load,
		save : save,
		set : set
	};
}());

ASOBAUDIO.libraryUtils = ( function() {

	var tracks = [];

	function error(errorMsg) {
		alert('libraryUtils error: ' + errorMsg);
	}

	function listTracks() {
		var medialist, newMediaList, i, trackElement, parent;
		console.log('listTracks length: ' + tracks.length);
		newMediaList = document.createElement('div');
		newMediaList.id = 'medialist';
		for( i = 0; i < tracks.length; i += 1) {
			trackElement = document.createElement('div');
			trackElement.className = 'trackinfo';
			trackElement.textContent = tracks[i].mediaId + ' | ' + tracks[i].title;
			newMediaList.appendChild(trackElement);
		}
		medialist = document.getElementById('medialist');
		console.log('medialist: ' + medialist);
		parent = medialist.parentNode;
		parent.removeChild(medialist);
		parent.appendChild(newMediaList);
	}

	function setTracksFromMediaLibrary(tracksJSONArr) {
		console.log('setTracks');
		console.log('setTracks JSON: ' + tracksJSONArr);
		if(ASOBAUDIO.ios) {
			tracks = tracksJSONArr;
		} else {
			tracks = JSON.parse(tracksJSONArr);
		}
		//console.log('setTracks parsing done.');
		this.listTracks();
	}

	function getTracksFromMediaLibrary() {
		console.log('getTracksFromMediaLibrary');
		if(ASOBAUDIO.ios) {
			ASOBAUDIO.iOSBridge.exec(this.setTracksFromMediaLibrary, this.error, 'getTracks');
		} else if(ASOBAUDIO.android) {
			//setTracksFromMediaLibrary(NativeInterface.getTracksFromMediaLibrary());
		} else {
			// Test code
			var testJSON = '[{"mediaId" : "3461054884673650595","book" : "Frank Herbert","book" : "Children of Dune book 3","title" : "Frank Herbert, Children of Dune (01 of 60)"},{"mediaId" : "17205042258315845504","book" : "Frank Herbert","book" : "Children of Dune book 3","title" : "Frank Herbert, Children of Dune (02 of 60)"},{"mediaId" : "16851954552807460291","book" : "Frank Herbert","book" : "Children of Dune book 3","title" : "Frank Herbert, Children of Dune (03 of 60)"},{"mediaId" : "16879714571355502305","book" : "Frank Herbert","book" : "Children of Dune book 3","title" : "Frank Herbert, Children of Dune (04 of 60)"},{"mediaId" : "17139704579671435219","book" : "Frank Herbert","book" : "Children of Dune book 3","title" : "Frank Herbert, Children of Dune (05 of 60)"}]';
			ASOBAUDIO.libraryUtils.setTracksFromMediaLibrary(testJSON);
		}
	}

	function init() {
		console.log('init');
		var mediaFolder;
		console.log('Trying to read file.');
				if (window.File && window.FileReader && window.FileList && window.Blob) {
					console.log('Success.');
				} else {
					console.log('Failure.');
				}
				
		// iOS will collect all tracks from the native media library.
		if(!ASOBAUDIO.ios) {
			mediaFolder = localStorage.getItem('mediaFolder');
			if(ASOBAUDIO.android) {
				if(mediaFolder) {
					console.log('------------mediaFolder: ' + mediaFolder);
					//NativeInterface.setMediaFolder(mediaFolder);
				} else {
					console.log('------------no mediafolder specified');
					//NativeInterface.listFolders();
				}
			} else {
				console.log('Trying to read file.');
				if (window.File && window.FileReader && window.FileList && window.Blob) {
					console.log('Success.');
				} else {
					console.log('Failure.');
				}
			}
		}
	}

	return {
		init : init,
		error : error,
		getTracksFromMediaLibrary : getTracksFromMediaLibrary,
		listTracks : listTracks,
		setTracksFromMediaLibrary : setTracksFromMediaLibrary
	};
}());

ASOBAUDIO.libraryUtils.init();

$(function() {
	$('#showTracksBtn').click(function() {
		ASOBAUDIO.libraryUtils.getTracksFromMediaLibrary();
	});

});
